/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.framework.sysid.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moon.admin.framework.sysid.entity.SysId;
import com.moon.admin.framework.sysid.mapper.SysIdMapper;
import com.moon.admin.framework.util.MoonException;
import org.springframework.stereotype.Service;

/**
 * id主键生成（暂时未考虑并发）
 * @author ninglong
 */
@Service
public class SysIdService extends ServiceImpl<SysIdMapper, SysId>{

	/**
	 * 根据业务生成id  id为8位数累加(每个业务都8位数)
	 */
	public int getId(String type){
		int minId = 10000000,maxId=99999999;
		SysId sysId = this.getById(type);
		//首次请求
		if(sysId==null){
			this.save(new SysId(type,minId));
			return minId;
		}else{
			sysId.setId(sysId.getId()+1);
			if(sysId.getId()>maxId){
				throw new MoonException("id主键生成失败，业务类型:【"+type+"】预先配制的数据范围已达上限，请先扩容");
			}
			this.updateById(sysId);
			return sysId.getId();
		}
	}
}